package com.xnyzc.lhy.common.util;

/**
 * @Author: huxuekuo
 * @Date: 2019-07-21 19:25
 * @Description:
 */
public class GpsUtil {


    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
    /**
     * 通过经纬度获取距离(单位：米)
     * @param lat1
     * @param lng1
     * @param lat2
     * @param lng2
     * @return
     */
    public static double getDistance(String lat1, String lng1, String lat2, String lng2) {
        double radLat1 = rad(Double.valueOf(lat1));
        double radLat2 = rad(Double.valueOf(lat2));
        double a = radLat1 - radLat2;
        double b = rad(Double.valueOf(lng1)) - rad(Double.valueOf(lng2));
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    /**
     * 经纬度校验
     * 经度longitude: (?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\\.([0-9]{6})
     * 纬度latitude：  (?:[0-9]|[1-8][0-9]|90)\\.([0-9]{6})
     * @return
     */
    public static boolean checkItude(String longitude,String latitude){
        String reglo = "((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,13}))|((?:180)\\.([0]{0,13}))";
        String regla = "((?:[0-9]|[1-8][0-9])\\.([0-9]{0,13}))|((?:90)\\.([0]{0,13}))";
        longitude = longitude.trim();
        latitude = latitude.trim();
        return longitude.matches(reglo) == true ? latitude.matches(regla) : false;
    }

    /**
     * 校验点位
     * @param point 点位 经度,纬度(119.2,40.2)
     * @return
     */
    public static boolean checkItudePoint(String point){
        String[] split = point.split(",");
        if(!(split.length == 2)){
            return false;
        }
        return checkItude(split[0],split[1]);
    }
}
